﻿#region Using directives

using System;
using System.ComponentModel;
using System.Collections;
using System.Xml.Serialization;
using System.Runtime.Serialization;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

#endregion

namespace SPL.Entities
{	
	///<summary>
	/// An object representation of the 'TVT' table. [No description found the database]	
	///</summary>
	/// <remarks>
	/// This file is generated once and will never be overwritten.
	/// </remarks>	
  [Serializable, DataObject]
  [CLSCompliant(true)]
	public partial class Tvt : TvtBase
	{
    #region private data
    private bool _isLoadPreData = false;
    private double _pre36 = 0;
    private double _pre39 = 0;
    private double _pre40 = 0;
    #endregion

		#region Constructors

		///<summary>
		/// Creates a new <see cref="Tvt"/> instance.
		///</summary>
		public Tvt():base(){}	
		
		#endregion

    #region Custom fields
    /// <summary>
    /// 
    /// </summary>
    [DescriptionAttribute(""), System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)]
    [DataObjectField(false, false, false)]
    public virtual System.String I1InputDateString
    {
      get
      {
        return this.I1InputDate.ToString("dd/MM/yyyy");
      }
    }

    /// <summary>
    /// 
    /// </summary>
    [DescriptionAttribute(""), System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)]
    [DataObjectField(false, false, false)]
    public virtual System.Double F36CondBHTon
    {
      get
      {
        return pre36;
      }
    }

    /// <summary>
    /// 
    /// </summary>
    [DescriptionAttribute(""), System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)]
    [DataObjectField(false, false, false)]
    public virtual System.Double F39LPGTon
    {
      get
      {
        return pre39;
      }
    }

    /// <summary>
    /// 
    /// </summary>
    [DescriptionAttribute(""), System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)]
    [DataObjectField(false, false, false)]
    public virtual System.Double F40CondNCSTon
    {
      get
      {
        return pre40;
      }
    }

    #region pre data
    /// <summary>
    /// 
    /// </summary>
    public virtual double pre36
    {
      get
      {
        if (!_isLoadPreData)
        {
          LoadPreData();
          _isLoadPreData = true;
        }
        return _pre36;
      }
    }
    /// <summary>
    /// 
    /// </summary>
    public virtual double pre39
    {
      get
      {
        if (!_isLoadPreData)
        {
          LoadPreData();
          _isLoadPreData = true;
        }
        return _pre39;
      }
    }
    /// <summary>
    /// 
    /// </summary>
    public virtual double pre40
    {
      get
      {
        if (!_isLoadPreData)
        {
          LoadPreData();
          _isLoadPreData = true;
        }
        return _pre40;
      }
    }

    #endregion
    #endregion

    #region Helper method

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    private void LoadPreData()
    {
      TimeSpan oneDay = new TimeSpan(1, 0, 0, 0, 0);
      DateTime yesterday = this.I1InputDate.Subtract(oneDay);

      string queryString = "SELECT * FROM TVT WHERE I1InputDate='" + yesterday.ToShortDateString() + "' AND Hid IS NULL";
      using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SPL.Data.ConnectionString"].ConnectionString))
      {
        SqlCommand command = new SqlCommand(queryString, connection);
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();
        try
        {
          if (reader.Read())
          {
            _pre36 = (double)reader["I36CondBhKhoiLuongTon"];
            _pre39 = (double)reader["I39BuproTon"];
            _pre40 = (double)reader["I40CondNcsKhoiLuongTon"];
          }
          else
          {
                _pre36 = 0;
                _pre39 = 0;
                _pre40 = 0;
          }
        }
        catch
        {
          _pre36 = 0;
          _pre39 = 0;
          _pre40 = 0;
        }
        finally
        {
          // Always call Close when done reading.
          reader.Close();
        }
      }
    }
    
    #endregion
	}
}
